package com.theatre.userservice.Controller;

import com.theatre.userservice.annotation.RequirePermission;
import com.theatre.userservice.mapper.RoleMapper;
import com.theatre.userservice.model.dto.RoleDTO;
import com.theatre.userservice.model.entity.Role;
import com.theatre.userservice.model.vo.Result;
import com.theatre.userservice.model.vo.RoleVO;
import com.theatre.userservice.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
@Tag(name = "角色接口", description = "提供角色相关的接口")
public class RoleController {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleService roleService;

    @RequirePermission("user:manage")
    @PostMapping("/insert")
    @Operation(summary = "新增角色", description = "新增角色")
    public Result<String> insert(@RequestBody RoleDTO roleDTO) {
        roleService.addRole(roleDTO);
        return Result.success();
    }

    @RequirePermission("user:manage")
    @GetMapping("/get/{id}")
    @Operation(summary = "查询角色", description = "获取角色")
    public Result<RoleVO> get(@PathVariable("id") Long id) {
        RoleVO roleVO = roleService.selectById(id);
        if (roleVO == null) {
            return Result.error("角色不存在");
        }
        return Result.success(roleVO);
    }

    @RequirePermission("user:manage")
    @PutMapping("/update")
    @Operation(summary = "更新角色", description = "更新角色")
    public Result<String> update(@RequestBody RoleDTO roleDTO) {
        roleService.updateById(roleDTO);
        return Result.success();
    }

    @RequirePermission("user:manage")
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色", description = "删除角色")
    public Result<String> delete(@PathVariable("id") Long id) {
        roleService.deleteById(id);
        return Result.success();
    }

    @RequirePermission("user:manage")
    @GetMapping("/list")
    @Operation(summary = "查询角色列表", description = "查询角色列表")
    public Result<List<RoleVO>> list() {
        List<Role> roles = roleMapper.selectList(null);
        List<RoleVO> roleVOS = new ArrayList<>();
        for (Role role : roles) {
            RoleVO roleVO = new RoleVO();
            roleVO.setId(role.getId());
            roleVO.setName(role.getName());
            roleVO.setDescription(role.getDescription());
            roleVOS.add(roleVO);
        }
        return Result.success(roleVOS);
    }

    @RequirePermission("user:manage")
    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除角色", description = "批量删除角色及关联表")
    public Result<String> deleteBatch(@RequestBody List<Long> roleIds) {
        if (roleIds == null || roleIds.isEmpty()) {
            return Result.error("角色ID列表不能为空");
        }
        int successCount = 0;
        List<Long> failedIds = new ArrayList<>();
        for (Long roleId : roleIds) {
            try {
                // 调用单个删除逻辑
                roleService.deleteById(roleId);
                successCount++;
            } catch (Exception e) {
                failedIds.add(roleId);
            }
        }
        if (failedIds.isEmpty()) {
            return Result.success("批量删除成功，共删除 " + successCount + " 个角色");
        } else {
            return Result.error("批量删除完成，但以下角色删除失败：" + failedIds.toString());
        }
    }
}
